package kr.or.kosta.otr.persistence.file;
import java.sql.SQLException;

import javax.annotation.Resource;

import kr.or.kosta.otr.vo.FileVO;

import org.springframework.stereotype.Repository;

import com.ibatis.sqlmap.client.SqlMapClient;

@Repository("FileDao")
public class FileDaoImpl implements FileDao{
	@Resource(name="sqlMapClient")
	private SqlMapClient sqlMapClient;

	@Override
	public void uploadFile(FileVO vo) throws SQLException {
		sqlMapClient.insert("File.uploadFile", vo);
	}

	@Override
	public void updateFile(FileVO vo) throws SQLException {
		sqlMapClient.update("File.updateFile", vo);
	}

	@Override
	public void deleteFile(int fileId) throws SQLException {
		sqlMapClient.delete("File.deleteFile", fileId);
	}

	@Override
	public String getOneFileName(int boardId) throws SQLException {
		String fileName="";
		if(sqlMapClient.queryForObject("File.getOneFileName", boardId)==null){
			fileName="";
		}else{
			fileName=(String) sqlMapClient.queryForObject("File.getOneFileName", boardId);
		}
		return fileName;
	}

	@Override
	public FileVO getOneFileByBoardId(int boardId) throws SQLException {
		return (FileVO) sqlMapClient.queryForObject("File.getOneFileByBoardId", boardId);
	}

	@Override
	public void deleteFileByBoardId(int boardId) throws SQLException {
		sqlMapClient.delete("File.deleteFileByBoardId", boardId);
	}

	@Override
	public int fileExist(int boardId) throws SQLException {
		int fe=(int) sqlMapClient.queryForObject("File.fileExist", boardId);
		if(sqlMapClient.queryForObject("File.getFileName", boardId)==null){
			fe=0;
		}
		return fe;
	}
}
